package com.priox.sigar.infraestructura.persistence.jpa.sigar;

import com.googlecode.genericdao.search.Search;
import com.mm.support.jpa.BaseDAO;
import com.priox.sigar.client.dto.GrupoEmpresarialDTO;
import com.priox.sigar.domain.model.GrupoEmpresarial;
import com.priox.sigar.domain.model.GrupoRepository;
import com.priox.sigar.server.infrastructure.persistence.util.WhereParams;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.stereotype.Repository;

import javax.annotation.PostConstruct;
import javax.sql.DataSource;
import java.util.ArrayList;
import java.util.List;


/**
 * User: aglwkrs
 * Date: 30/11/11
 */
@Repository
public class GrupoRepositoryImpl extends BaseDAO<GrupoEmpresarial, Long> implements GrupoRepository {

    private final static Logger logger = LoggerFactory.getLogger(GrupoRepositoryImpl.class);

    private NamedParameterJdbcTemplate parameterJdbcTemplate;
    private JdbcTemplate jdbcTemplate;


    @Autowired
    DataSource dataSource;

    @PostConstruct
    public void init() {
        this.parameterJdbcTemplate = new NamedParameterJdbcTemplate(dataSource);
        this.jdbcTemplate = new JdbcTemplate(dataSource);
    }

    public List<GrupoEmpresarial> findByIds(List<Long> ids) {
        if (ids.isEmpty()) return new ArrayList<GrupoEmpresarial>();
        Search s = new Search();
        s.addFilterIn("id", ids);
        return search(s);
    }

    public List<GrupoEmpresarial> findByNameList(String name) {
        Search s = new Search();
        if (StringUtils.isNotBlank(name)) {
            s.addFilterILike("descripcion", name.toUpperCase() + "%");

        }
        s.addSortAsc("descripcion");
        return search(s);


    }

    public List<GrupoEmpresarial> findByFilter(GrupoEmpresarial filter) {
        Search s = new Search();

        if (StringUtils.isNotBlank(filter.getNombre())) {
            s.addFilterILike("descripcion", "%" + filter.getNombre() + "%");
        }
        return search(s);
    }

    public void saveCadena(GrupoEmpresarial cadena) {
        this.persist(cadena);
    }

    public GrupoEmpresarial findBy(Long code) {
        Search s = new Search();
        if (code != null) {
            s.addFilterEqual("id", code);
        }
        return searchUnique(s);
    }

    public void removeSociedadGrupo(Long id) {
        jdbcTemplate.update("delete from TM_SOCIEDAD where grupoEmpresarial_id = ? ",
                new Object[]{id});
    }

    public GrupoEmpresarial findByName(String value) {
        Search s = new Search();
        s.addFilterEqual("descripcion", value);
        return searchUnique(s);
    }


    public List<GrupoEmpresarialDTO> findByNameGrupo(String s) {
        List<GrupoEmpresarialDTO> items = null;
        StringBuffer sql = new StringBuffer();
        sql.append(" SELECT ");
        sql.append(" grupo_empresarial_id \"id\",");
        sql.append(" NOMBRE \"nombre\" ");
        sql.append(" FROM SGTM_GRUPO_EMPRESARIAL ");
        sql.append(" WHERE 1=1 ");
        WhereParams params = new WhereParams();
        if (StringUtils.isNotBlank(s)) {
            sql.append(params.filter(" and  UPPER(DESCRIPCION) like :razonSocial ", "%" + s));
        }
        return parameterJdbcTemplate.query(sql.toString(), params.getParams(), new BeanPropertyRowMapper<GrupoEmpresarialDTO>(GrupoEmpresarialDTO.class));
    }

    public List<GrupoEmpresarial> findByRUC(String ruc) {
        Search s = new Search();
        if (ruc != null) {
            s.addFilterEqual("ruc", ruc);
        }

        return search(s);
    }


}
